package com.sky.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.IShoppingCartService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 购物车 服务实现类
 * </p>
 *
 * @author wt
 * @since 2025-10-21
 */
@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements IShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        Long currentId = BaseContext.getCurrentId();//获取当前用户id

        Long setmealId = shoppingCartDTO.getSetmealId();//获取套餐id
        String dishFlavor = shoppingCartDTO.getDishFlavor();//获取菜品口味
        Long dishId = shoppingCartDTO.getDishId();//获取菜品id
        ShoppingCart shoppingCart = new ShoppingCart();//创建购物车对象
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);//拷贝数据

        shoppingCart.setUserId(currentId);//设置用户id
        List<ShoppingCart> shoppingCartList = baseMapper.selectList(new LambdaQueryWrapper<ShoppingCart>()
                .eq(currentId != null, ShoppingCart::getUserId, currentId)
                .eq(dishId != null, ShoppingCart::getDishId, dishId)
                .eq(setmealId != null, ShoppingCart::getSetmealId, setmealId)
                .eq(dishFlavor != null, ShoppingCart::getDishFlavor, dishFlavor)
        );
        if (shoppingCartList != null && shoppingCartList.size() == 1) {
            //如果已经存在就数量加1
            shoppingCart = shoppingCartList.get(0);
            shoppingCart.setNumber(shoppingCart.getNumber() + 1);
            baseMapper.updateById(shoppingCart);
        } else {
            //如果不存在就插入数据，数量就是1
            //判断插入当前购物车的是套餐还是菜品
            if (dishId != null) {
                Dish dish = dishMapper.selectById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            } else {
                Setmeal setmeal = setmealMapper.selectById(shoppingCart.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            baseMapper.insert(shoppingCart);
        }

    }

    @Override
    public List<ShoppingCart> showShoppingCart() {
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.selectList(
                new LambdaQueryWrapper<ShoppingCart>()
                        .eq(ShoppingCart::getUserId, BaseContext.getCurrentId())
        );
        return shoppingCartList;
    }

    @Override
    public void cleanShoppingCart() {
        shoppingCartMapper.delete(new LambdaQueryWrapper<ShoppingCart>()
                .eq(ShoppingCart::getUserId, BaseContext.getCurrentId()));
    }

    @Override
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();//创建购物车对象
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);//拷贝shoppingCartDTO给shoppingCart

        shoppingCart.setUserId(BaseContext.getCurrentId());

        List<ShoppingCart> shoppingCartList = shoppingCartMapper.selectList(new LambdaQueryWrapper<ShoppingCart>()
                .eq(shoppingCart.getUserId()!=null,ShoppingCart::getUserId, shoppingCart.getUserId())
                .eq(shoppingCart.getDishId()!=null,ShoppingCart::getDishId, shoppingCart.getDishId())
                .eq(shoppingCart.getSetmealId()!=null,ShoppingCart::getSetmealId, shoppingCart.getSetmealId())
                .eq(shoppingCart.getDishFlavor()!=null,ShoppingCart::getDishFlavor, shoppingCart.getDishFlavor())
        );
        if (shoppingCartList != null && shoppingCartList.size() > 0) {
            ShoppingCart shoppingCart1 = shoppingCartList.get(0);
            if (shoppingCart1.getNumber() == 1) {
                shoppingCartMapper.deleteById(shoppingCart1);
            } else {
                shoppingCart1.setNumber(shoppingCart1.getNumber() - 1);
                shoppingCartMapper.updateById(shoppingCart1);
            }
        }
    }
}
